package com.lizx.persional.resource;


import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.plugins.Page;
import com.lizx.persional.common.utils.HttpContextUtils;
import com.lizx.persional.common.utils.Result;
import com.lizx.persional.entity.Resources;
import com.lizx.persional.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * api
 * </p>
 *
 * @author 李振兴
 * @since 2018-02-27
 */
@RestController
@RequestMapping("/persional/resources")
public class ResourcesResource {

    @Autowired
    private ResourcesService resourcesService;

    /**
     * 资源列表
     * @param page
     * @param resources
     * @return
     */
    @Permission("resources:menu:list")
    @GetMapping("/page")
    public Result page(Page<Resources> page, Resources resources) {
        return Result.success(resourcesService.selectPage(page));
    }

    /**
     * 新增
     * @param resources
     * @return
     */
    @PostMapping("/insert")
    public Result insert(Resources resources) {
        return Result.success(resources.insert());
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Result getResources(@PathVariable Long id) {
        return Result.success(resourcesService.selectById(id));
    }

    /**
     * 修改
     * @param resources
     * @return
     */
    @PutMapping("/update")
    public Result update(Resources resources) {
        return Result.success(resources.updateById());
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(List<Long> ids) {
        return Result.success(resourcesService.deleteBatchIds(ids));
    }

    /**
     * 根据用户id获取菜单列表
     *
     * @return
     */
    @Permission("resources:menu:list")
    @GetMapping("/menu/list")
    public Result selectMenuListByUserId() {
        Long userId = Long.valueOf(SSOHelper.getSSOToken(HttpContextUtils.getHttpServletRequest()).getId());
        return Result.success(resourcesService.selectMenuByUserId(userId));
    }
}

